var equControlBoardJs = function(targetObj){
	this.sysUtils = new kendoUtils();
	this.targetObj = targetObj;
	this.init();
	return this;
};

equControlBoardJs.prototype={
    init:function(){
        var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
        var bindFunctionArray = [{fName:"saveequOrg",fs:this.saveequOrg},{fName:"cancel",fs:parkTree.resetTree}];
        this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
        this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
        this.initNumberWidget();
        this.loadEquinfo();
    },
    /**
	 * 创建数字化控件
	 */
	createNumericTextBox:function(div,minNumber,maxNumber){
		div.kendoNumericTextBox({
			min:minNumber,
			max:maxNumber,
			format:"#",
            change:this.changeDeviceNumberByInput,
            spin:this.changeDeviceNumberBySpin
		});
	},
	/**
	 * 初始化数字控件
	 */
	initNumberWidget:function(){
        /*this.createNumericTextBox(this.targetObj.find("#deviceNumber"),1,99);
        this.createNumericTextBox(this.targetObj.find("#port"),0,65535);
		this.createNumericTextBox(this.targetObj.find("#clientPort"),0,65535);
		this.createNumericTextBox(this.targetObj.find("#serverPort"),0,65535);
		this.createNumericTextBox(this.targetObj.find("#centerPort"),0,65535);
		this.createNumericTextBox(this.targetObj.find("#synch"),0,60000,600);
		this.createNumericTextBox(this.targetObj.find("#heartbeat"),0,255,2);*/
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_synch",600);
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_ip",'192.168.0.30');
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_ndsIp",'192.168.0.1');
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_gatewayIp",'192.168.0.1');
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_subnetMask",'255.255.255.0');
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_heartbeat",2);
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_port",5000);
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_clientIp",'192.168.0.100');
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_clientPort",5000);
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_serverIp",'192.168.0.101');
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_serverPort",5000);
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_centerIp",'192.168.0.102');
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_centerPort",5000);
		this.sysUtils.mvvmModel.set("equipmentManagements_basic_greeting",'欢迎使用智能停车系统');
	},
    changeDeviceNumberByInput:function () {
        var no = this.value();
        var _this = this;
        var code = controlBoardJs.editResult.organizationCode == undefined ? '':controlBoardJs.editResult.organizationCode;
        JsonpAjax(baseApiPath+'facilities/checkCtrlNo?ctrlNo='+no+'&organizationCode='+code,null,"json",function(result){
            if(result.success){
                layer.tips('设备机号'+no+'已存在！！！',_this._text[0],{tipsMore: true,tips: [2, '#78BA32']});
                _this._text[0].value = '';
            }
        });
    },
    changeDeviceNumberBySpin:function () {
        var no = this.value();
        var _this = this;
        var code = controlBoardJs.editResult.organizationCode == undefined ? '':controlBoardJs.editResult.organizationCode;
        JsonpAjax(baseApiPath+'facilities/checkCtrlNo?ctrlNo='+no+'&organizationCode='+code,null,"json",function(result){
            if(result.success){
                layer.tips('设备机号'+no+'已存在！！！',_this.element[0],{tipsMore: true,tips: [2, '#78BA32']});
                _this.element[0].value = '';
            }
        });
    },
    loadEquinfo:function (e) {
        var that = this;
        that.editResult = null;
        that.isEdit = null;
        if (!isEmpty(e)) {
            that = $(e.target).data("that");
        }
        var id = zTree.getSelectedNodes()[0].id,level=zTree.getSelectedNodes()[0].level;
        var url = baseApiPath+"facilities/parkInfo";
        if(id){
            that.isEdit = true;
            url += '?isEdit='+that.isEdit+'&organizationCode='+id;
        }else{
            that.isEdit = false;
            var node = zTree.getSelectedNodes()[0].getParentNode();
            url = baseApiPath+'facilities/addOrz?isEdit='+that.isEdit+'&pid='+node.id+'&lev='+level+'&gpc=equ';
        }
        //显示相应的设备信息
        Utils.createAjaxDropDownList({div:that.targetObj.find("#deviceType"),select:[{code:parkTree.downDevJson["deviceType"],name:parkTree.downDevJson["deviceName"]}],unshift:false,change:that.channelChange},that);
        that.sysUtils.mvvmModel.set("equipmentManagements_basic_deviceid",parkTree.downDevJson["deviceType"]);
        JsonpAjax(url,null,"json",function(result){
            that.editResult = result;
            that.sysUtils.setVariantBindFieldValue(result);
            that.setswitchCss();
            that.showOff();
        },false);
        parkTree.hideMask();
    },
    setswitchCss:function () {
        var that = this;
        that.targetObj.find(".checkbox input[type='checkbox']").each(function () {
            $(this).click(function(){
                this.value= this.value !=1 ? 1 : 0;
                if(this.id == 'offline'){
//						that.showOff();
                    var offline = that.targetObj.find('#offline')[0].checked;
                    that.selectDisable(offline);
                }
            });
            if(this.value == 1){
                this.checked=true;
            }else{
                this.value=0;
                this.checked=false;
            }
            var bindS = $(this).attr('data-bind');
            that.sysUtils.mvvmModel.set(bindS.substr(bindS.indexOf(':')+1,bindS.length),this.value);
        });
    },
    selectDisable:function (offline) {
        var $this = this;
        if(offline){
            $this.targetObj.find('#isOffline').show();
        }else{
            $this.targetObj.find('#isOffline').hide();
        }

    },
    //保存设备信息
    saveequOrg:function(e){
        var that =  $(e.target).data("that");
        if(validityForm(that.targetObj)){
            var parkInfo = that.sysUtils.getBindDocJson();
            var url = baseApiPath+"facilities/saveParkInfo?isEdit=" + that.isEdit;
            var sendJson=parkInfo;
            for(var o in parkInfo){
                if(o.indexOf('_') !=-1){
                    var strArray = o.substring(o.indexOf('_')+1);
                    var name = o.substring(0, o.indexOf('_'));
                    var jsonA = {};
                    jsonA[name] = that.recursionObjectName(strArray,parkInfo[o]) ;
                    delete parkInfo[o];
                    sendJson =$.extend(true,parkInfo,jsonA);
                }
            }
            var offline = that.targetObj.find('#offline')[0].checked;
            if(!offline){
                delete sendJson['equipmentManagements']['basic']['clientIp'];
                delete sendJson['equipmentManagements']['basic']['clientPort'];
                delete sendJson['equipmentManagements']['basic']['serverIp'];
                delete sendJson['equipmentManagements']['basic']['serverPort'];
                delete sendJson['equipmentManagements']['basic']['centerIp'];
                delete sendJson['equipmentManagements']['basic']['centerPort'];
                delete sendJson['equipmentManagements']['basic']['synch'];
                delete sendJson['equipmentManagements']['basic']['heartbeat'];
            }
            JsonpAjax(url,JSON.stringify(sendJson),"json",function(result){
                    that.sysUtils.setVariantBindFieldValue(result);
                    that.isEdit=true;
                    parkTree.localUpdate(result);
            });
        }
    },
    recursionObjectName : function (strArray,val) {
        if (strArray.indexOf('_') == -1){
            var jsonC = {};
            jsonC[strArray]=val;
            return jsonC;
        }
        var name = strArray.substring(0, strArray.indexOf('_'));
        strArray = strArray.substring(strArray.indexOf('_')+1);
        var jsonB = {};
        jsonB[name] = this.recursionObjectName(strArray,val);
        return jsonB;
    },
    showOff:function () {

        var flag = this.targetObj.find('#offline').val() == 'on' ? false : this.targetObj.find('#offline').val() == 0 ? false : true;
        if(flag){
            $('#isOffline').show();

        }else{
            $('#isOffline').hide();
        }
    },
    channelChange:function () {
        var flag = this.dataItem(this.select()).vFlag == '1' ? true : false;
        if(flag){
            $('#one').show();
        }else{
            $('#one').hide();
        }
    }
}
